package com.hly.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.common.utils.PageUtils;
import com.hly.common.utils.Query;
import com.hly.wms.dao.WareSkuDao;
import com.hly.wms.entity.SkuInfoEntity;
import com.hly.wms.entity.WareSkuEntity;
import com.hly.wms.feign.ProductFeign;
import com.hly.wms.service.WareSkuService;
import com.hly.wms.vo.SkuHasStockVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private WareSkuDao wareSkuDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String wareId = (String) params.get("wareId");
        String skuId = (String) params.get("skuId");

        System.out.println("wareId = "+wareId+"  skuId = "+skuId);

        if(StringUtils.isNotEmpty(wareId) && !wareId.equals("undefined") && !wareId.equals("null") && !wareId.equals("")){
            if(StringUtils.isNotEmpty(skuId) && !skuId.equals("undefined")&& !skuId.equals("null") && !skuId.equals("")){
                wrapper.and(i -> i.eq("sku_id",skuId).eq("ware_id",wareId));
            }else{
                wrapper.eq("ware_id", wareId);
            }
        }else {
            if(StringUtils.isNotEmpty(skuId) && !skuId.equals("undefined") && !skuId.equals("null") && !skuId.equals("")) {
                wrapper.eq("sku_id", skuId);
            }
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 先根据 skuId 和 ware_id 查询 是否拥有这个用户
        List<WareSkuEntity> list = this.list(new QueryWrapper<WareSkuEntity>().and(i -> i.eq("sku_id", skuId).eq("ware_id", wareId)));
        //没有这个用户 那就新增
        if(list.size() == 0 || list == null){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            // 根据属性值设置
            try{
                SkuInfoEntity bySkuId = productFeign.getBySkuId(skuId);
                if(bySkuId != null){
                    wareSkuEntity.setSkuName(bySkuId.getSkuName());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            wareSkuEntity.setStock(skuNum);
            this.save(wareSkuEntity);
        }else{
            // 有该记录那就进行更新,这就没重复的sku_id和ware_id  stock=stock+#{skuNum} WHERE sku_id=#{skuId} AND ware_id=#{wareId}
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }

    }

    @Override
    public List<SkuHasStockVo> jubStockBySkuId(List<Long> skuIds) {
        List<SkuHasStockVo> skuHasStockVos = this.list(new QueryWrapper<WareSkuEntity>().in("sku_id",skuIds)).stream().map(wareSkuEntity -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            skuHasStockVo.setSkuId(wareSkuEntity.getSkuId());
            if ((wareSkuEntity.getStock() != null || wareSkuEntity.getStockLocked() != null) && wareSkuEntity.getStock() - wareSkuEntity.getStockLocked() > 0) {
                skuHasStockVo.setHasStock(true);
            } else {
                skuHasStockVo.setHasStock(false);
            }
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return skuHasStockVos;
    }

}